<?php
class user_menu {

	var $CI;
	public function __construct() {
		$this->CI =& get_instance();
	}
	/**
	 *
	 *
		* @name get_user_menu
		* @date 2013-1-9
		* @author eifel
		* @param
		* @return
		* @version 1.0
		* @description 获取用户的权限信息
	 */
	public function get_user_menu() {
		$this->CI->load->model("common/adm_role_model");
		$user_id 			=$_SESSION["user_id"];
		$user_level_info	=$this->CI->adm_role_model->get_role_level($user_id);

		if ($user_level_info['level']==1) {
			$menu_info	=$this->CI->adm_role_model->query_admin_menu();
		}else{
			$menu_info	=$this->CI->adm_role_model->query_user_menu($user_id);
		}
		return $this->format_web_menu($menu_info);

	}
	/**
	 *
	 *
		* @name format_web_menu
		* @date 2013-1-9
		* @author eifel
		* @param 用户的权限信息数组
		* @return string web_menu
		* @version 1.0
		* @description 把权限信息数组转换成页面中的nav
	 */
	private  function format_web_menu($menu_info) {
		$menu 	='<div id="accordion" class="sidebar-offcanvas">';
		/*循环获取ul*/
		$menu_num =count($menu_info);
		for ($i = 0; $i < $menu_num; $i++) {
			if ($menu_info[$i]['parent_id']==0) {
				$nav		='<div class="panel panel-default">';
				$nav		.='<div class="panel-heading">';
				$nav		.='<h4 class="panel-title">';
				$nav		.='<a data-toggle="collapse" href="#collapse'.$i.'">'.$menu_info[$i]['name'].'</a>';
				$nav		.='</h4>';
				$nav		.='</div>';
				$nav		.='<div id="collapse'.$i.'" class="list-group panel-collapse collapse in">';
				/*循环获取li*/
				for ($j = 0; $j < $menu_num; $j++) {
					if ($menu_info[$j]['parent_id']==$menu_info[$i]['right_id']) {
						$open_target="null";
						if ($this->check_ul_open($menu_info[$j]['url'])) {
							$open_target="active";
						}
						$nav.='<a href="'.base_url().'index.php/'.$menu_info[$j]['url'].'" class="list-group-item '.$open_target.'">'.$menu_info[$j]['name'].'</a>';
					}
				}
				$nav		.='</div>';
				$nav		.='</div>';
				$menu		.=$nav;
			}
		}
		$menu.='</div>';

		return $menu;
	}
	/**
	 *
	 *
		* @name check_ul_open
		* @date 2013-1-10
		* @author eifel
		* @param
		* @return
		* @version 1.0
		* @description 判断是否需要打开ul
	 */
	private function check_ul_open($url){
		if ($url!="") {
			if (stristr($_SERVER['PATH_INFO'],str_ireplace("index.php", "", $url))){
				return true;
			}
		}

	}


}